package com.imagine;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.PlatformTransactionManager;
import javax.jms.Queue;
import java.util.ArrayList;
import java.util.Arrays;

@EnableAutoConfiguration
@Configuration
@ComponentScan
@EnableJms
@SpringBootApplication
@EnableScheduling
@MapperScan("com.imagine.mapper")
public class Application extends SpringBootServletInitializer {
    private static Logger logger = Logger.getLogger(Application.class);
    //DataSource配置
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {
        return new DataSource();
    }

    //提供SqlSeesion
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));

        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }



    @Bean
    public Queue queue1() {
        return new ActiveMQQueue("stock_search_queue");
    }

    @Bean
    public Queue queue2() {
        return new ActiveMQQueue("delegation_apply_queue");
    }

    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory (){
        ActiveMQConnectionFactory activeMQConnectionFactory =
                new ActiveMQConnectionFactory(
                        ActiveMQConnectionFactory.DEFAULT_USER,
                        ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                        "tcp://127.0.0.1:61616");
        activeMQConnectionFactory.setTrustedPackages(new ArrayList(Arrays.asList("com.imagine.entity")));
        return activeMQConnectionFactory;
    }

    @Bean
    public JmsMessagingTemplate jmsMessagingTemplate(){
        JmsMessagingTemplate jt = new JmsMessagingTemplate();
        SingleConnectionFactory scf = new SingleConnectionFactory();
        scf.setTargetConnectionFactory(activeMQConnectionFactory());
        scf.setReconnectOnException(true);//连接断开后重试
        jt.setConnectionFactory(scf);
        return jt;
    }
    /**
     * Main Start
     */
    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
        logger.info("============= SpringBoot Start Success =============");
    }


}